package faceb.server.service.impl;

import java.io.File;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.google.gson.Gson;

import faceb.server.dao.CourseDao;
import faceb.server.dao.UserDao;
import faceb.server.model.Course;
import faceb.server.model.User;
import faceb.server.service.UserService;
import faceb.server.util.ServerFinal;
import faceb.server.util.UUIDUtil;

@Service("userService")
public class UserServiceImpl implements UserService {
	@Resource(name = "userDao")
	private UserDao userDao;
	
	@Resource(name = "courseDao")
	private CourseDao courseDao;

	public void register(String email, String pass, HttpServletRequest req,
			HttpServletResponse resp) {
		if (email != null && pass != null) {
			long time = System.currentTimeMillis();
			String userid = String.valueOf(time);
			User u = new User(userid, pass, "m", email, email, time, "默认电话号码",
					"默认真实姓名", "默认信息", "默认qq", "默认博客");
			int result = userDao.register(u);
			mkdirUserDirs(userid);
			String courseStr = initCourseString();   //初始化用户课程
			String c_id = UUIDUtil.getCode(20);
			courseDao.insertCourse(new Course(c_id, 1, userid, courseStr)); 
			if (result == 1) {
				req.setAttribute("message", "注册成功");

			} else {
				req.setAttribute("message", "注册失败");
			}
			

		}

	}

	/**
	 * 创建用户相关目录
	 * 
	 * @param userid
	 */
	private void mkdirUserDirs(String userid) {
		File userRoot = Paths.get(ServerFinal.RESOURCE_DIR, userid).toFile();
		File userPic = Paths.get(userRoot.getAbsolutePath(), "pics").toFile();
		File userRes = Paths.get(userRoot.getAbsolutePath(), "resources")
				.toFile();
		userRoot.mkdirs();
		userPic.mkdirs();
		userRes.mkdirs();

	}

	public void login(String email, String pass, HttpServletRequest req,
			HttpServletResponse resp) {

		// 校验参数是否岑存在
		if (email == null || pass == null) {
			try {
				req.getRequestDispatcher("/tpl/login").forward(req, resp);
				return;
			} catch (Exception e) {

			}
		}

		User u = userDao.login(email, pass);
		// 校验邮箱和密码是否匹配
		if (u == null) {
			try {
				req.getRequestDispatcher("/tpl/login").forward(req, resp);
				return;
			} catch (Exception e) {

			}
		}
		// 如果匹配设置session,跳转至用户信息页面
		req.getSession().setAttribute("user", u);
		try {
			Cookie cookie = new Cookie("userid", u.getU_id());
			resp.addCookie(cookie);
			resp.sendRedirect("/faceb-cupt/tpl/center/users/" + u.getU_id());
			// req.getRequestDispatcher("/tpl/center").forward(req, resp);
			return;
		} catch (Exception e) {

		}

	}

	public String initCourseString(){
		Map<String,String> map = new HashMap<String, String>();
		Gson gson = new Gson();
		for (int i = 1; i <=7; i++) {
			for (int j = 1; j <=6; j++) {
			map.put("_"+i+"_"+j, "");	
			}
		}
		return gson.toJson(map);
	}
	public static void main(String[] args) {
		new UserServiceImpl().mkdirUserDirs("666");
	}
}